#include "snake.h"
#include "ui_snake.h"
#include"QPainter"
#include"mypushbutton.h"

Snake::Snake(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Snake)
{
    ui->setupUi(this);

    //设置开始界面
    this->setFixedSize(960,540);
    this->setWindowIcon(QIcon(":/res/Snake_window.png"));
    this->setWindowTitle("贪吃蛇");

    //创建开始按键
    MyPushButton *startBtn = new MyPushButton(":/res/start_button.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5 - startBtn->width()*0.5,this->height()*0.35);

    //音乐
    backgroundMusic = new QSoundEffect(this);
    backgroundMusic->setSource(QUrl::fromLocalFile(":/res/backgroundMusic.wav"));
    //循环播放
    backgroundMusic->setLoopCount(QSoundEffect::Infinite); //循环播放
    backgroundMusic->setVolume(0.1f);
    //开始播放
    backgroundMusic->play();

    //实例化游戏进行画面
    playwindow = new PlayWindow;

    connect(startBtn, &QPushButton::clicked, this, [=]()
            {

                // 自身隐藏
                this->hide();
                // 显示选择关卡场景
                playwindow->show();

            });

}

//绘制开始界面
void Snake::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);
    QPixmap pix;
    //加载图片
    pix.load(":/res/Snake_window.png");
    //绘制背景图
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}






Snake::~Snake()
{
    delete ui;
}


